# Who Cares if You Vote? Partisan agreement and injunctive norms of voting
# Edward Fieldhouse (1), David Cutts (2), Jack Bailey (1)
# (1) The University of Manchester, (2) The University of Birmingham


# 1. Housekeeping ---------------------------------------------------------

# The following code requires that you have first opened the associated
# project file. If not, click the button on the top right and open it
# ("Open Project...").

# Note also that the Bayesian methods we use below rely on having installed
# the probabilistic programming language Stan. If you have yet to install
# Stan, please see https://mc-stan.org/users/interfaces/.

# Load packages

library(tidyverse)
library(magrittr)
library(jbmisc)   # devtools::install_github("jackobailey/jbmisc)
library(haven)
library(here)


# Load data from the British Election Study Internet Panel. Note: If you
# plan to use this data, please make sure that you download the most recent
# version available at https://www.britishelectionstudy.com.

dta <- read_sav(here("_data", "BES2017_W14_Panel_v0.4.sav.zip"))


# Merge in wave 4 randomisation variable.

dta <- 
  left_join(
    dta,
    read_dta(here("_data", "randvar_w4.dta.zip")),
    by = "id"
  )



# 2. Transform data -------------------------------------------------------

# Select variables

dta <- 
  dta %>% 
  select(
    # Respondent-level covariates
    id,
    wave2,
    wave4,
    pid = partyIdW2,
    rnd = rnd_var,
    
    # Discussant-level covariates
    vote = discussantVoteName1W2,
    vote_1W4 = discussantVoteName1W4,
    vote_2W4 = discussantVoteName2W4,
    vote_3W4 = discussantVoteName3W4
  )


# Party ID

dta$pid <-
  dta$pid %>%
  as_factor() %>%
  factor(
    labels = c("Conservative",
               "Labour",
               "Liberal Democrat",
               "SNP",
               "Plaid Cymru",
               "UKIP",
               "Green Party",
               "BNP",
               "Other",
               "None",
               "Don't know")
  )


# Discussant vote intention

dta$vote <-
  dta$vote %>%
  as_factor() %>%
  factor(
    labels = 
      c("Labour",
        "Conservative",
        "Liberal Democrat",
        "SNP",
        "Plaid Cymru",
        "Green Party",
        "UKIP",
        "BNP",
        "Other",
        "None",
        "Don't know")
  )


# Create dyadic partisanship variable

opt <- c("None", "Don't know")

dta <- 
  dta %>% 
  mutate(
    dyad =
      case_when(
        pid == vote & !(pid %in% opt) & !(vote %in% opt) & pid != "Other" & vote != "Other" ~ "Shared Partisanship",
        pid != vote & !(pid %in% opt) & !(vote %in% opt) ~ "Opposing Partisanship",
        pid == "Other" & vote == "Other" ~ "Opposing Partisanship",
        pid %in% opt & !(vote %in% opt) ~ "Respondent Non-partisan",
        vote %in% opt & !(pid %in% opt) ~ "Discussant Non-partisan",
        pid %in% opt & vote %in% opt ~ "Shared Non-partisan"
      ) %>% 
      factor(
        levels = 
          c(
            "Shared Non-partisan",
            "Shared Partisanship",
            "Opposing Partisanship",
            "Respondent Non-partisan",
            "Discussant Non-partisan"
          )
      )
  )


# Count wave 4 discussants and drop vars

dta <- 
  dta %>% 
  mutate(
    discW4 = 
      3 %>% 
      subtract(
        dta[, paste0("vote_", 1:3, "W4")] %>% 
          is.na() %>% 
          rowSums()
      ) %>% 
      ifelse(rnd == 1, ., NA)
  ) %>% 
  mutate(discW4 = ifelse(discW4 > 0, 1, 0)) %>% 
  select(
    -vote_1W4,
    -vote_2W4,
    -vote_3W4
  )



# 3. Create tables --------------------------------------------------------

# Create wave 4 table

w4_table <- 
  dta %>% 
  group_by(dyad) %>% 
  summarise(
    `W4 Survey Non-Response` = scales::percent(1 - round(mean(wave4), 3), accuracy = 0.1),
    `W4 Item Non-Response` = scales::percent(1 - round(mean(discW4, na.rm = T), 2), accuracy = 0.1),
    .groups = "drop"
  ) %>% 
  filter(is.na(dyad) == F) %>% 
  rename(`Dyadic Partisanship` = dyad)


# Save to disk

write_csv(w4_table, here("_output", "attritionW4.csv"))

